//
//  ZTControlTopView.m
//  YSSmartHome
//
//  Created by Yahaong on 16/8/12.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "ZTControlTopView.h"
#import "MultipleControlButton.h"
#import "AudioToolbox/AudioToolbox.h"
#import "UIButton+ButtonStyle.h"
#import "GlobalConst.h"

@interface ZTControlTopView ()

@property(nullable,nonatomic, strong)UIButton *m_oneChannel;
@property(nullable,nonatomic, strong)UIButton *m_twoChannel;
@property(nullable,nonatomic, strong)UIButton *m_threeChannel;
@property(nullable,nonatomic, strong)UIButton *m_fourChannel;
@property(nullable,nonatomic, strong)UIImageView* m_imgVLine;
@property(nullable,nonatomic, strong)UIImageView* m_imgHLine;
@end
@implementation ZTControlTopView

- (instancetype)initWithDevType:(ZTDeviceType)devType
{
    if (self = [super init])
    {
        _devType = devType;
        self.backgroundColor = RGBColor(0, 111, 195);
        [self setChnnelButtonIconWithType:devType];
        [self setupUIWithType:devType];
    }
    return self;
}


- (void)setupUIWithType:(ZTDeviceType)type
{
    switch (type)
    {
        case ZTDeviceTypeOneWay0Switch:
        case ZTDeviceTypeOneWaySwitch:
        case ZTDeviceTypeZTDoorsensor:
        case ZTDeviceTypeInfraredDetector:
        case ZTDeviceTypeOneWaySocket:
        case ZTDeviceTypeSmoke:
        case ZTDeviceTypeAir:
        case ZTDeviceTypeGAS:
        case ZTDeviceTypeFlooding:
        case ZTDeviceTypeAlarmkey:
        case ZTDeviceTypeCurtainSingleTrack:
        {
            [self setupOneChannelUI];
        }
            break;
        case ZTDeviceTypeTwoWaySocket:
        case ZTDeviceTypeTwoWay0Switch:
        case ZTDeviceTypeTwoWaySwitch:
        case ZTDeviceTypeCurtainDoubleTrack:
        {
            [self setupTwoChannelUI];
        }
            break;
        case ZTDeviceTypeThreeWaySocket:
        case ZTDeviceTypeThreeWay0Switch:
        case ZTDeviceTypeThreeWaySwitch:
        {
            [self setupThreeChannelUI];
        }
            break;
        case ZTDeviceTypeFourWaySocket:
        case ZTDeviceTypeFourWay0Switch:
        case ZTDeviceTypeFourWaySwitch:
        {
            [self setupFourChannelUI];
        }
            break;
        default:
            break;
    }
}


- (void)setChnnelButtonIconWithType:(ZTDeviceType)devType
{
    switch (devType)
    {
        case ZTDeviceTypeOneWaySocket:
        {
            [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_socket_normal"] forState:UIControlStateNormal];
            [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_socket_open"] forState:UIControlStateSelected];
        }
            break;
        case ZTDeviceTypeOneWay0Switch:
        case ZTDeviceTypeOneWaySwitch:
        {
            [self.m_oneChannel setImage:[UIImage imageNamed:@"bigSwichClose"] forState:UIControlStateNormal];
            [self.m_oneChannel setImage:[UIImage imageNamed:@"bigSwichOpen"] forState:UIControlStateSelected];
        }
            break;
        case ZTDeviceTypeTwoWay0Switch:
        case ZTDeviceTypeTwoWaySwitch:
        case ZTDeviceTypeThreeWay0Switch:
        case ZTDeviceTypeThreeWaySwitch:
        case ZTDeviceTypeFourWay0Switch:
        case ZTDeviceTypeFourWaySwitch:
        {
            [self.m_oneChannel setImage:[UIImage imageNamed:@"smallSwichClose"] forState:UIControlStateNormal];
            [self.m_oneChannel setImage:[UIImage imageNamed:@"smallSwichOpen"] forState:UIControlStateSelected];
            [self.m_twoChannel setImage:[UIImage imageNamed:@"smallSwichClose"] forState:UIControlStateNormal];
            [self.m_twoChannel setImage:[UIImage imageNamed:@"smallSwichOpen"] forState:UIControlStateSelected];
            [self.m_threeChannel setImage:[UIImage imageNamed:@"smallSwichClose"] forState:UIControlStateNormal];
            [self.m_threeChannel setImage:[UIImage imageNamed:@"smallSwichOpen"] forState:UIControlStateSelected];
            [self.m_fourChannel setImage:[UIImage imageNamed:@"smallSwichClose"] forState:UIControlStateNormal];
            [self.m_fourChannel setImage:[UIImage imageNamed:@"smallSwichOpen"] forState:UIControlStateSelected];
            [self.m_oneChannel setTitle:@"灯一" forState:UIControlStateNormal];
            [self.m_twoChannel setTitle:@"灯二" forState:UIControlStateNormal];
            [self.m_threeChannel setTitle:@"灯三" forState:UIControlStateNormal];
            [self.m_fourChannel setTitle:@"灯四" forState:UIControlStateNormal];
        }
            break;
        case ZTDeviceTypeGAS:
        case ZTDeviceTypeSmoke:
        {
            [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_smoke_gas_normal"] forState:UIControlStateNormal];
            [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_smoke_gas_open"] forState:UIControlStateSelected];
            [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_smoke_gas_offline"] forState:UIControlStateDisabled];
        }
            break;
        case ZTDeviceTypeZTDoorsensor:
        {
            {
                [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_doorsensor_normal"] forState:UIControlStateNormal];
                [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_doorsensor_open"] forState:UIControlStateSelected];
            }
            break;
        }
        case ZTDeviceTypeInfraredDetector:
        {
            {
                [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_infraredDetector_normal"] forState:UIControlStateNormal];
                [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_infraredDetector_open"] forState:UIControlStateSelected];
            }
            break;
        }
        case ZTDeviceTypeAlarmkey:
        {
            [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_alarm_normal"] forState:UIControlStateNormal];
            [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_alarm_open"] forState:UIControlStateSelected];
        }
            break;
        case ZTDeviceTypeFlooding:
        {
            [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_flooding_normal"] forState:UIControlStateNormal];
            [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_flooding_open"] forState:UIControlStateSelected];
        }
            break;
        case ZTDeviceTypeCurtainSingleTrack:
        {
            [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_curtain_one_normal"] forState:UIControlStateNormal];
            [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_curtain_one_open"] forState:UIControlStateSelected];
        }
            break;
        case ZTDeviceTypeCurtainDoubleTrack:
        {
            [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_curtain_one_normal"] forState:UIControlStateNormal];
            [self.m_oneChannel setImage:[UIImage imageNamed:@"home_ctl_curtain_one_open"] forState:UIControlStateSelected];
            [self.m_twoChannel setImage:[UIImage imageNamed:@"home_ctl_curtain_one_normal"] forState:UIControlStateNormal];
            [self.m_twoChannel setImage:[UIImage imageNamed:@"home_ctl_curtain_one_open"] forState:UIControlStateSelected];
        }
            break;
        
        default:
            break;
    }

}
- (void)setupOneChannelUI
{
    [self addSubview:self.m_oneChannel];
    [self.m_oneChannel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.equalTo(CGSizeMake(kIphoneSize_Widith(270), kIphoneSize_Height(270)));
    }];
}
- (void)setupTwoChannelUI
{
    [self addSubview:self.m_imgVLine];
    [self.m_imgVLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.width.equalTo(1);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        
    }];
    [self addSubview:self.m_oneChannel];
    [self.m_oneChannel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).multipliedBy(0.5);
        make.centerY.equalTo(self);
        make.size.equalTo(CGSizeMake(128, 140));
    }];
    [self addSubview:self.m_twoChannel];
    [self.m_twoChannel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).multipliedBy(1.5);
        make.centerY.equalTo(self);
        make.size.equalTo(self.m_oneChannel);
    }];
}

- (void)setupThreeChannelUI
{
    [self addSubview:self.m_imgHLine];
    [self.m_imgHLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.height.equalTo(1);
        make.leading.trailing.equalTo(self);
    }];
    
    [self addSubview:self.m_imgVLine];
    [self.m_imgVLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.width.equalTo(1);
        make.top.equalTo(self);
        make.bottom.equalTo(self.m_imgHLine.top);
    }];
    
    [self addSubview:self.m_oneChannel];
    [self.m_oneChannel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).multipliedBy(0.5);
        make.centerY.equalTo(self).multipliedBy(0.5);
        make.size.equalTo(CGSizeMake(128, 140));
    }];
    
    [self addSubview:self.m_twoChannel];
    [self.m_twoChannel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).multipliedBy(1.5);
        make.centerY.equalTo(self).multipliedBy(0.5);
        make.size.equalTo(self.m_oneChannel);
    }];
    
    [self addSubview:self.m_threeChannel];
    [self.m_threeChannel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self).multipliedBy(1.5);
        make.size.equalTo(self.m_oneChannel);
    }];
}

- (void)setupFourChannelUI
{
    [self addSubview:self.m_imgHLine];
    [self.m_imgHLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.height.equalTo(1);
        make.leading.trailing.equalTo(self);
        
    }];
    [self addSubview:self.m_imgVLine];
    [self.m_imgVLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.width.equalTo(1);
        make.top.bottom.equalTo(self);
        
    }];
    [self addSubview:self.m_oneChannel];
    [self.m_oneChannel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).multipliedBy(0.5);
        make.centerY.equalTo(self).multipliedBy(0.5);
        make.size.equalTo(CGSizeMake(kIphoneSize_Widith(135), kIphoneSize_Height(160)));
    }];
    [self addSubview:self.m_twoChannel];
    [self.m_twoChannel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).multipliedBy(1.5);
        make.centerY.equalTo(self).multipliedBy(0.5);
        make.size.equalTo(self.m_oneChannel);
        
    }];
    [self addSubview:self.m_threeChannel];
    [self.m_threeChannel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).multipliedBy(0.5);
        make.centerY.equalTo(self).multipliedBy(1.5);
        make.size.equalTo(self.m_oneChannel);
    }];
    [self addSubview:self.m_fourChannel];
    [self.m_fourChannel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).multipliedBy(1.5);
        make.centerY.equalTo(self).multipliedBy(1.5);
        make.size.equalTo(self.m_oneChannel);
    }];

}
- (void)layoutSubviews
{
    [super layoutSubviews];
    if (self.devType == ZTDeviceTypeThreeWaySocket
        || self.devType == ZTDeviceTypeThreeWaySwitch
        || self.devType == ZTDeviceTypeThreeWay0Switch)
    {
        [self.m_oneChannel layoutButtonWithEdgeInsetsStyle:ButtonEdgeInsetsStyleTop imageTitleSpace:-5];
        [self.m_twoChannel layoutButtonWithEdgeInsetsStyle:ButtonEdgeInsetsStyleTop imageTitleSpace:-10];
        [self.m_threeChannel layoutButtonWithEdgeInsetsStyle:ButtonEdgeInsetsStyleTop imageTitleSpace:-10];
    }
    
}
- (void)controlClick:(UIButton *)btn
{
    if (self.devType != ZTDeviceTypeInfraredDetector
        && self.devType != ZTDeviceTypeAlarmkey
        && self.devType != ZTDeviceTypeInfraredDetector)
    {
        if ([self checkShockIsOpen])
        {
           AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        }
    }
    if (self.block)
    {
        self.block(btn.tag, btn.selected);
    }
}
- (void)setChannel:(NSInteger )index status:(BOOL)statu
{
    UIButton *btn = [self viewWithTag:index];
    btn.selected = statu;
}

- (BOOL) checkShockIsOpen
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:kZTShockSwitchIsOpen];
}
#pragma mark - getters and setters

- (UIButton *)m_oneChannel
{
    if (!_m_oneChannel) {
        if (self.devType == ZTDeviceTypeOneWaySwitch
            || self.devType == ZTDeviceTypeOneWay0Switch
            || self.devType == ZTDeviceTypeTwoWaySwitch
            || self.devType == ZTDeviceTypeTwoWay0Switch
            || self.devType == ZTDeviceTypeThreeWaySwitch
            || self.devType == ZTDeviceTypeThreeWay0Switch
            || self.devType == ZTDeviceTypeFourWaySwitch
            || self.devType == ZTDeviceTypeFourWay0Switch)
        {
            _m_oneChannel = [[MultipleControlButton alloc]init];
        }else
        {
            _m_oneChannel = [[UIButton alloc]init];
        }
        _m_oneChannel.tag = 1;
        _m_oneChannel.adjustsImageWhenDisabled = NO;
        [_m_oneChannel addTarget:self action:@selector(controlClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _m_oneChannel;
}

- (UIButton *)m_twoChannel
{
    if (!_m_twoChannel) {
        if (self.devType == ZTDeviceTypeOneWaySwitch
            || self.devType == ZTDeviceTypeOneWay0Switch
            || self.devType == ZTDeviceTypeTwoWaySwitch
            || self.devType == ZTDeviceTypeTwoWay0Switch
            || self.devType == ZTDeviceTypeThreeWaySwitch
            || self.devType == ZTDeviceTypeThreeWay0Switch
            || self.devType == ZTDeviceTypeFourWaySwitch
            || self.devType == ZTDeviceTypeFourWay0Switch)
        {
            _m_twoChannel = [[MultipleControlButton alloc]init];
        }else
        {
            _m_twoChannel = [[UIButton alloc]init];
        }
        _m_twoChannel.tag = 2;
        _m_twoChannel.adjustsImageWhenDisabled = NO;
        [_m_twoChannel addTarget:self action:@selector(controlClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _m_twoChannel;
}
- (UIButton *)m_threeChannel
{
    if (!_m_threeChannel) {
        if (self.devType == ZTDeviceTypeOneWaySwitch
            || self.devType == ZTDeviceTypeOneWay0Switch
            || self.devType == ZTDeviceTypeTwoWaySwitch
            || self.devType == ZTDeviceTypeTwoWay0Switch
            || self.devType == ZTDeviceTypeThreeWaySwitch
            || self.devType == ZTDeviceTypeThreeWay0Switch
            || self.devType == ZTDeviceTypeFourWaySwitch
            || self.devType == ZTDeviceTypeFourWay0Switch)
        {
            _m_threeChannel = [[MultipleControlButton alloc]init];
        }else
        {
            _m_threeChannel = [[UIButton alloc]init];
        }
        _m_threeChannel.tag = 3;
        _m_threeChannel.adjustsImageWhenDisabled = NO;
        [_m_threeChannel addTarget:self action:@selector(controlClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _m_threeChannel;
}

- (UIButton *)m_fourChannel
{
    if (!_m_fourChannel)
    {
        if (self.devType == ZTDeviceTypeOneWaySwitch
            || self.devType == ZTDeviceTypeOneWay0Switch
            || self.devType == ZTDeviceTypeTwoWaySwitch
            || self.devType == ZTDeviceTypeTwoWay0Switch
            || self.devType == ZTDeviceTypeThreeWaySwitch
            || self.devType == ZTDeviceTypeThreeWay0Switch
            || self.devType == ZTDeviceTypeFourWaySwitch
            || self.devType == ZTDeviceTypeFourWay0Switch)
        {
            _m_fourChannel = [[MultipleControlButton alloc]init];
        }else
        {
            _m_fourChannel = [[UIButton alloc]init];
        }
        _m_fourChannel.tag = 4;
        _m_fourChannel.adjustsImageWhenDisabled = NO;
        [_m_fourChannel addTarget:self action:@selector(controlClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _m_fourChannel;
}

- (UIImageView *)m_imgVLine
{
    if (!_m_imgVLine) {
        _m_imgVLine  = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"splitLine"]];
    }
    return _m_imgVLine;
}
- (UIImageView *)m_imgHLine
{
    if (!_m_imgHLine) {
        _m_imgHLine  = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"vectorsplit"]];
    }
    return _m_imgHLine;
}
@end
